//
//  ConNewClubVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/8.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "ConNewClubVc.h"
#import "TYCyclePagerView.h"
#import "TYCyclePagerViewCell.h"
#import "CommendClubCell.h"
#import "NewClubCell.h"
#import "ConNewAllClubVc.h"
#import "ConNewClubDetailVc.h"
#import "NewHomeNetwork.h"
#import "MBProgressHUD+MJ.h"
#import <MJRefresh.h>
#import "BannerModel.h"
#import "ConGoodsDetailController.h"
#import "ConNearAcInfoViewController.h"
#import "NewBecomeVipVc.h"

@interface ConNewClubVc ()<TYCyclePagerViewDataSource,TYCyclePagerViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) TYCyclePagerView *pagerView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *roolImgs;
@property (nonatomic,strong) NSArray *rImgs;
@property (nonatomic,strong) NSMutableArray *clubLists;

@end

@implementation ConNewClubVc

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self getBannerList];
    self.rImgs = @[@"lunbo1",@"lunbo2",@"lunbo3"];
    [self setUpTableView];
    [self getClubList];
}
- (void)setUpTableView
{
    [self.view addSubview:self.tableView];
    [self setRoolView];
    self.tableView.tableHeaderView = self.pagerView;
}
#pragma ----------------tableView-----------------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.clubLists.count == 0) return 0;
    if (self.clubLists.count < 2) return 1;
    return self.clubLists.count-1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 55+(ScreenW-55)/2.0*122/192+25;
    }
    return ScreenW*0.35*738/1280+20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        CommendClubCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommendClubCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.clubLists.count > 0) {
            cell.oneModel = self.clubLists[0];
        }
        if (self.clubLists.count > 2) {
            cell.twoModel = self.clubLists[1];
        }
        __WEAK
        cell.allClubShowBlock = ^{
            ConNewAllClubVc *vc = [[ConNewAllClubVc alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        cell.clubClickBlock = ^(ClubModel *model) {
            [self jumpToDetailWithModel:model];
        };
        return cell;
    }
    NewClubCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NewClubCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.clubLists[indexPath.row+1];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) return;
    [self jumpToDetailWithModel:self.clubLists[indexPath.row+1]];
}
#pragma ----------------轮播图-----------------------------------
- (void)setRoolView
{
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]init];
    pagerView.layer.borderWidth = 1;
    pagerView.layer.borderColor = RGBColorOne(240).CGColor;
    pagerView.isInfiniteLoop = YES;
    pagerView.autoScrollInterval = 3.0;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    // registerClass or registerNib
    [pagerView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:NSStringFromClass([TYCyclePagerViewCell class])];
    self.pagerView = pagerView;
}
#pragma mark - TYCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.rImgs.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    TYCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([TYCyclePagerViewCell class]) forIndex:index];
//    cell.model = self.roolImgs[index];
    cell.imagePlace = self.rImgs[index];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(ScreenW-50, (ScreenW-50)*102/272);
    layout.itemSpacing = 8;
    //layout.minimumAlpha = 0.3;
    layout.minimumScale = 0.9;
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index
{
    if (index != 2) {
        NewBecomeVipVc *vipVc = [[NewBecomeVipVc alloc]init];
        [self.navigationController pushViewController:vipVc animated:YES];
    }
//    BannerModel *model = self.roolImgs[index];
//    if (model.type == 1) {//商品
//        ConGoodsDetailController *goodsVc = [[ConGoodsDetailController alloc]init];
//        goodsVc.productNo = model.ID;
//        [self.navigationController pushViewController:goodsVc animated:YES];
//    }else if (model.type == 2) {//活动
//        ConNearAcInfoViewController *infoVc = [[ConNearAcInfoViewController alloc]init];
//        infoVc.ID = [model.ID integerValue];
//        [self.navigationController pushViewController:infoVc animated:YES];
//    }else {//人脉
//
//    }
}
//获取轮播图后再加载tableview
- (void)getBannerList
{
    [self showWaiter];
    [NewHomeNetwork getBannerListWithParame:nil response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        if (success) {
            [self.roolImgs addObjectsFromArray:lists];
            [self setUpTableView];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
//推荐俱乐部
- (void)getClubList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"pageNo"] = @"1";
    parame[@"isRecommend"] = @"1";
    [self showClearBgWaiter];
    [NewHomeNetwork getClubListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        [self.tableView .mj_header endRefreshing];
        [self hideNoDataLab];
        if (success) {
            if (lists.count == 0) {
                [self showNoDataLab:self.view range:CGRectMake(0, self.view.height*0.5-30, ScreenW, 20) title:@"暂无数据,请尝试下拉刷新"];
                return ;
            }
            [self.clubLists addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
            if ([msg isEqualToString:NetworkError]) {
                [self showNoDataLab:self.view range:CGRectMake(0, self.view.height*0.5-30, ScreenW, 20) title:@"暂无数据,请尝试下拉刷新"];
            }
        }
    }];
}
- (void)jumpToDetailWithModel:(ClubModel *)model
{
    ConNewClubDetailVc *detailVc = [[ConNewClubDetailVc alloc]init];
    detailVc.ID = model.ID;
    [self.navigationController pushViewController:detailVc animated:YES];
}


- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.pagerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), (ScreenW-50)*102/272+16);
    self.pagerView.layout.layoutType = 1;
    self.tableView.frame = self.view.bounds;
}
#pragma --------------------------------------------------------
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CommendClubCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([CommendClubCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NewClubCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([NewClubCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self.clubLists removeAllObjects];
//            [self getBannerList];
            [self getClubList];
        }];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}

- (NSMutableArray *)clubLists
{
    if (!_clubLists) {
        _clubLists = [NSMutableArray array];
    }
    return _clubLists;
}
- (NSMutableArray *)roolImgs
{
    if (!_roolImgs) {
        _roolImgs = [NSMutableArray array];
    }
    return _roolImgs;
}

@end
